﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MShop.Web.Components.Extensions
{
    public static class EnumerableExtensions
    {
        public static IList<SelectListItem> ConvertListItem<T>(this IEnumerable<T> querable, string text, string value, object selectedValue, bool includeAll)
        {
            SelectList list = new SelectList(querable, value, text, selectedValue);
            List<SelectListItem> enumerable = list.ToList();
            if (includeAll)
            {
                enumerable.Insert(0, new SelectListItem() { Text = "所有", Value = "-1" });
            }
            return enumerable;
        }

        public static IList<SelectListItem> ConvertListItem<T>(this IEnumerable<T> querable, string text, string value, object selectedValue)
        {
            return ConvertListItem<T>(querable, text, value, selectedValue, false);
        }

        public static IList<SelectListItem> ConvertListItem<T>(this IEnumerable<T> querable, string text, string value)
        {
            return ConvertListItem<T>(querable, text, value, null, false);
        }
    }
}